package com.liy.cloud.base.rest;

import javax.validation.Valid;

import com.liy.cloud.base.entity.BaseEquTypeEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.liy.cloud.base.service.BaseEquTypeService;
import com.das.framework.common.annotation.ActionPermission;
import com.das.framework.common.annotation.MethodPermission;
import com.das.framework.common.constants.PermissionCodes;
import com.das.framework.common.extvo.ResultVO;
import com.das.framework.common.vo.PageVO;
import com.das.framework.common.web.controller.BaseCrudController;

/**
 * @Author: zhangw
 * @Date: 2018/6/15  14:08
 * @Description: 设备种类管理
 **/
@RestController
@RequestMapping("equtype")
@ActionPermission(code = "BASE.EQUTYPE", description = "设备种类管理")
public class BaseEquTypeRest extends BaseCrudController<BaseEquTypeService, BaseEquTypeEntity> {

    protected static Logger log = LoggerFactory.getLogger(BaseEquTypeRest.class);

    @Value("${server.port}")
    private String port;
    @Autowired
    private BaseEquTypeService baseEquTypeService;


    @RequestMapping( {"/select/typecode" , "/select/typecode/{pageSize}", "/select/typecode/{pageSize}/{pageNo}" })
    @MethodPermission(codes = PermissionCodes.SEARCH, description = "列表查询")
    public PageVO<BaseEquTypeEntity> queryByTypeCode(@RequestParam(value = "typeCode", required = true) String typeCode,
                                                     @PathVariable(value = "pageSize",required = false) Integer pageSize,
                                                     @PathVariable(value = "pageNo",required = false) Integer pageNo ) {
        PageVO<BaseEquTypeEntity> page = new PageVO();
        if (pageNo != null) {
            page.setPageNo(pageNo);
        }
        if (pageSize != null) {
            page.setPageSize(pageSize);
        }
        try {
            baseEquTypeService.selectByTypeCode(typeCode, page);
        } catch (Exception e) {
            log.error("查询数据出错!", e);
            page.setStatus("500");
            page.setMessage("查询时发生错误:" + e.getMessage());
        }
        return page;
    }


    @RequestMapping(value = {"/addBaseEquType"}, method = {RequestMethod.POST})
    @MethodPermission(codes = {"INSERT"}, description = "新增")

    public ResultVO addBaseEquType(@RequestBody @Valid BaseEquTypeEntity baseEquTypeEntity) {

        try {
            //新增前先校验编号是否重复
            BaseEquTypeEntity equTyep = this.baseService.findById(baseEquTypeEntity.getId());
            if(equTyep != null) {
                return new ResultVO(false, "种类编号存在重复，请重新输入", "500");
            }

            baseEquTypeService.addBaseEquType(baseEquTypeEntity);
            return new ResultVO(true, "新增成功", "200", baseEquTypeEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new ResultVO(false, "新增时发生错误:" + e.getMessage(), "500");
        }
    }

}
